package com.zxa.dao;

import com.zxa.pojo.Product;
import com.zxa.util.DBUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.stereotype.Repository;
import sun.nio.cs.FastCharsetProvider;

import javax.swing.text.TabExpander;
import javax.xml.transform.Templates;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zxa on 17-7-22.
 */
@Repository
public class ProductImpl extends RedisGeneratorDao<String,Product> implements ProductDao
{

    private static final String KEY = "product";

    private static boolean flag = true;

    public Map getAllFromDB(){
        Statement statement = null;
        ResultSet set = null;
        Map<String,Product> map = new HashMap<String, Product>();
        try {
            statement = DBUtil.getConnection().createStatement();

            set = statement.executeQuery("select * from product");

            while (set.next()){

                Product p = new Product(set.getInt(1),set.getString(2),
                        set.getString(4),set.getDouble(3),
                        set.getDate(5),set.getString(6));
                map.put(p.getId()+"",p);
                System.out.println(p.getProName());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        flag = false;
        redisTemplate.opsForHash().putAll(KEY, map);
        return map;
    }




    public Map<String,Product> getAll()
    {
        if(flag){
            System.out.println("first");
        }else
        {
            System.out.println("not first");
        }

        Map<String,Product> map = addAll();
        return map;
    }

    public Product get(String hashKey)
    {
        return (Product) redisTemplate.opsForHash().get(KEY,hashKey);
    }

    public void delete(String hashKey)
    {
        redisTemplate.opsForHash().delete(KEY,hashKey);
    }



    public void addOrUpdate(Product value)
    {
        redisTemplate.opsForHash().put(KEY,value.getId()+"",value);
    }

    public Map addAll(){
        return redisTemplate.opsForHash().entries(KEY);
    }

    public void deleteAll()
    {
        redisTemplate.delete(KEY);
    }

    //    public List<Product> getAll()
//    {
//
//        return null;
//    }
//
//    public Product get(String key)
//    {
//        return redisTemplate.opsForValue().get(key);
//    }
//
//    public void delete(String key)
//    {
//        redisTemplate.opsForValue().getOperations().delete(key);
//        System.out.println("删除成功！");
//
//    }
//
//    public void update(Product p)
//    {
//        redisTemplate.opsForValue().set(p.getId()+"",p);
//        System.out.println("修改成功");
//    }
//
//    public void add(final Product value)
//    {
//
//
//        redisTemplate.opsForValue().set(value.getId()+"",value);
//
//
//        System.out.println("添加成功!!!");
//
//
//    }
}
